package com.wfx.controller;


import com.wfx.entity.*;
import com.wfx.service.IWxbGoodsService;
import com.wfx.service.IWxbGoodsSkuService;
import com.wfx.service.impl.WxbGoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>title: com.wfx.controller</p>
 * author zhuximing
 * description:
 */
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {
    @Autowired
    private IWxbGoodsService wxbGoodsService;
    @Autowired
    private IWxbGoodsSkuService wxbGoodsSkuService;

    @RequestMapping("save")
    public Result save (@RequestBody GoodsSkuDTO dto){
        System.out.println(dto);
        //1.先插入goods信息，生成主键id（审核、是否删除 给默认值）
        //2.添加sku（sku标题=skuname+规格和规格选项，createtime、updatetime
        Result result = wxbGoodsService.insertGood(dto.getGoods());
        wxbGoodsSkuService.insertSku(dto.getSkus(),result);
        return new Result(true,"保存成功");
    }

    @PostMapping("/search")
    public PageResult<WxbGoods> search(@RequestBody PageDTO<WxbGoods> pageDTO){
        return wxbGoodsService.searchMerchant(pageDTO);
    }

    @PostMapping("delById")
    public Result delOne(@RequestBody WxbGoods wxbGoods){
        Result result1 = wxbGoodsService.delOne(wxbGoods);
        Result result = wxbGoodsSkuService.delOne(wxbGoods.getId());
        if(!result.getSuccess()){
            return result;
        }
        if (!result1.getSuccess()){
            return result1;
        }
        return new Result(true,"删除成功");
    }

}